package com.shirodemo.service.impl;

import com.shirodemo.mapper.UserMapper;
import com.shirodemo.pojo.User;
import com.shirodemo.pojo.UserExample;
import com.shirodemo.service.UserService;
import com.shirodemo.util.ColUtil;
import com.shirodemo.util.IDUtils;
import com.shirodemo.util.PasswordHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * Created by 追风少年
 *
 * @email doubihah@foxmail.com
 * @create 2017/12/13 11:41
 **/
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Override
    public void createUser(User user) {
        String uuid = IDUtils.createUUID();
        user.setPkId(uuid);
        String encryptPassword = PasswordHelper.encryptPassword(user);
        user.setPassword(encryptPassword);
        userMapper.insert(user);
    }

    @Override
    public void changePassword(String userId, String newPassword) {

    }

    @Override
    public void correlationRoles(String userId, String... roleIds) {

    }

    @Override
    public void uncorrelationRoles(String userId, String... roleIds) {

    }

    @Override
    public User findByUsername(String username) {
        User user = null;
        // 创建查询条件
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        // 根据条件查询
        List<User> users = userMapper.selectByExample(example);
        if(!ColUtil.isEmpty(users)) user = users.get(0);
        return user;
    }

    @Override
    public Set<String> findRoles(String username) {
        return null;
    }

    @Override
    public Set<String> findPermissions(String username) {
        return null;
    }
}
